/**
 * 当天出币主播排行榜
 */

import { toSuccess, useEntityManager } from "@sker/core"
import { useData } from "@sker/runtime"
import dayjs from "dayjs"
import { z } from "zod"

const sql = (start: string) => `select 
sum(count) as money_count, 
count(1) as device_count,
max(count) as max_count,
device."owner" as mobile,
log.user_id  as user_id ,
account.name as name
from ks_live_room_log as log 
left join ks_device as device on device.uuid = log.uuid
left join ks_account as account on account.user_id = log.user_id 
where log.status = '1' and open_time > '${start}' 
group by device.owner, log.user_id , account.name
order by max_count desc
limit 120`

export default async () => {
    const input = z.object({})
    const body = input.parse(useData())
    return await useEntityManager(async m => {
        const start = dayjs().startOf('day').format('YYYY-MM-DD HH:mm:ss')
        const list = await m.query(sql(start))
        return toSuccess(list)
    })
}
